//Cmills, 2/18/11, Take Nulls out of Array
package myjavaapplication1;
import java.math.*;
import java.util.ArrayList; //Necessary import
import java.util.List;
import java.io.*;
import java.util.*;
import java.io.*;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class cleanArray {

public static Object [] cleanArrayMethod (Object []input)
{
List output=new ArrayList();

for(int i=0;i<input.length;i++)
{
if(input[i]!=null)
{
output.add(input[i]);
}
}


return (output.toArray()); //returns an array
}

public static String [] returnArrayRow(String input) //format output string (e.g ...) to Array
    {
    String s=input;
   
    int SensorTypeOne=7; //Number of one sensor type
    int SensorTypeTwo=5; //Number of another sensor type
    int SensorTypeThree=1;
    int numSensors=SensorTypeOne+SensorTypeTwo+SensorTypeThree;
    int ValuesOne=3; //Values returned for sensor type one (e.g. 3=Voltage,Current,Temp)
    int ValuesTwo=1; //Values returned for another sensor type
    int ValuesThree=1;
    String delimSensor="#";
    String delimTime=";11";
    String delimEnd=";";
    String delim=",";
    String nothing="#N/A";

    int len=1+27+SensorTypeOne*ValuesOne+SensorTypeTwo*ValuesTwo+SensorTypeThree*ValuesThree; //num values (1 is timestamp)
     String[]str=new String[len];
    int[] index=new int[len];
    String[] value=new String[len];

      try{
        
    //int len=lenStr(s,delimSensor,SensorTypeOne,ValuesOne,SensorTypeTwo,ValuesTwo);
    for(int num=1;num<10;num++)
    {


    String command=""+num;
    }

    for(int num=30;num<31;num++)
    {
     String command=""+num;


    }

    for (int num1=1;num1<3;num1++)
    {
        for(int num2=0;num2<10;num2++)
        {
        String command=""+num1+""+num2;
       
        if(s.contains(";"+command))
        {
           
        }
            
        }
        }



    if(s.length()>0){  //Check first element of string for command digit (e.g 17,18)
        String first=""+s.charAt(0);
        if(!((first.equals("0"))||(first.equals("1"))||(first.equals("2"))||(first.equals("3")))){
            s="";
        }}
     if(s.length()>1){
        String first=""+s.charAt(0);
        if(!((first.equals("0"))||(first.equals("1"))||(first.equals("2"))||(first.equals("3")))){
            s="";
        }
        String second=""+s.charAt(1);
        if(!((second.equals(delim))||(second.equals("0"))||(second.equals("1"))||(second.equals("2"))||(second.equals("3"))||(second.equals("4"))||(second.equals("5"))||(second.equals("6"))||(second.equals("7"))||(second.equals("8"))||(second.equals("9")))){
            s="";
     }}
      if(s.length()>2) //comma check
      {
          String second=""+s.charAt(1);
           String third=""+s.charAt(2);
           if(!(second.equals(delim)||third.equals(delim)))
           {
      s="";
           }
        }

           if(s.length()>1)   //Adds to 'other' if not command type 17 or 18 or 11
            {
              String first=""+s.charAt(0);
             String second=""+s.charAt(1);
             if((!first.equals("1"))||(!(second.equals("7")||second.equals("8")||second.equals("1"))))
             {

                 value[len-1]=s;
                }
             if(first.equals("1")&&second.equals("1")&&s.contains(delimEnd)) //timestamp NEED DELIM AT END
             {
               value[0]=s.substring(3,s.indexOf(delimEnd));
             }
                 }

    

  
  
   


    for(int k=0;k<s.length();k++)
{
        if((""+s.charAt(k)).equals(delimSensor)){ //e.g. #7,
			int next=0;
                        String sensor="";
			   		if((""+s.charAt(k+2)).equals(delim)){ //1-9
                                                sensor=""+s.charAt(k+1);} 
                                            //next=Integer.parseInt(""+s.charAt(k+1));

                                        else {//10-13
                                            sensor=""+s.substring(k+1,k+3); }
                                            //next=Integer.parseInt(s.substring(k+1,k+3));
                                                 
	
            if(sensor.length()>0){  //Check first element of string for command digit (e.g 1, 18, 30)
        String first=""+sensor.charAt(0);
        if(!((first.equals("1"))||(first.equals("2"))||(first.equals("3"))||(first.equals("4"))||(first.equals("5"))||(first.equals("6"))||(first.equals("7"))||(first.equals("8"))||(first.equals("9")))){
            sensor="";
        }}
         
            
     if(sensor.length()>1){
         String first=""+sensor.charAt(0);
        if(!(first.equals("1"))){
            sensor="";
        }
         String second=""+sensor.charAt(1);
        if(!((second.equals(delim))||(second.equals("0"))||(second.equals("1"))||(second.equals("2"))||(second.equals("3")))){
            sensor="";
     }}          
     
       if(sensor.length()>(""+numSensors).length()){
           sensor="";
       }
       
     if(sensor.length()>0) //continues if sensor value is okay                                  
     {
         next=Integer.parseInt(sensor);
         
                                         
            if(next<=SensorTypeOne)
                {
                int stop=0;

     for(int j=k+1;j<s.length();j++)
    {
	 String seq=""+s.charAt(j);
//	 System.out.println(seq);
        if(seq.equals(delimSensor)||seq.equals(delimEnd))
    {
		  stop=j-1;
		  break;
    }
	 }
	// System.out.println(stop);
	int marker=k+2+(""+next).length(); //extract data
    String data="";
    if(marker<stop){
      data=s.substring(marker,stop);
                    }
 else
     return returnArrayRow(""); //null

              String[] temp = data.split(","); //Placing Data in Array--HARD CODE based on ValuesOne, etc....
    //*	for(int r=0;r<temp.length;r++){
	//*		System.out.print(temp[r]+"/");}
	//*	System.out.println();
			//for(int z=0;z<temp.length;z++)
				//System.out.println(temp[z]);
		  int spot=(next-1)*ValuesOne;
        int leng=temp.length;
        if (leng>ValuesOne){
            leng=ValuesOne;
        }
      
       for (int h=0;h<leng;h++)
        {
              int checker=1; //check if each sensor return value is valid
  for(int jk=0;jk<temp[h].length();jk++)
  {
     int asciival=(int)temp[h].charAt(jk);
    if((asciival<=47)||(asciival>=58)){
        checker=0;
    }
  }
  if (temp[h].equals(""))
  {
      checker=0;
  }
      
  //System.out.println(temp[h])
  if(checker==1){
       Double d=Double.parseDouble(temp[h])/100;
        value[spot+h+1]=""+d;}
  else if (checker==0){
      
     value[spot+h+1]="#N/A";
  }
  
        }


                }
                else if(next > SensorTypeOne && next <=SensorTypeOne+SensorTypeTwo)
                {
  //*  System.out.println(next);
	             int stop=0;
    for(int j=k+1;j<s.length();j++)
    {
        if((""+s.charAt(j)).equals(delimSensor))
  {
	     stop=j-1;
		 break;
	
  }
		 else if((""+s.charAt(j)).equals(delimEnd))
		 {
		   stop=j;
		 break;
		 }

    }
	 int marker=k+2+(""+next).length();
    String data="";
    if(marker<stop){
      data=s.substring(marker,stop);
                    }
 else
     return null;

              String[] temp = data.split(","); //Placing Data in Array--HARD CODE based on ValuesOne, etc....
        int spot=ValuesOne*SensorTypeOne+(next-SensorTypeOne-1)*ValuesTwo;
        int leng=temp.length;
        if (leng>ValuesTwo){
            leng=ValuesTwo;
        } for (int h=0;h<leng;h++)
        {
              int checker=1; //check if each sensor return value is valid
  for(int jk=0;jk<temp[h].length();jk++)
  {
     int asciival=(int)temp[h].charAt(jk);
    if((asciival<=47)||(asciival>=58)){
        checker=0;
    }
  }
  if (temp[h].equals(""))
  {
      checker=0;
  }
      
  //System.out.println(temp[h])
  if(checker==1){
       Double d=Double.parseDouble(temp[h])/100;
        value[spot+h+1]=""+d;}
  else if (checker==0){
      
     value[spot+h+1]="#N/A";
  }
  
        }

//2438, 18B20, 2406 ('0k' or 'nok')
//7,1,5
        //18B20--19 and temp (e.g 2400)
                }
                 
   else if(next > SensorTypeOne+SensorTypeTwo)
                {
                   
  //*  System.out.println(next);
	             int stop=0;
    for(int j=k+1;j<s.length();j++)
    {
        if((""+s.charAt(j)).equals(delimSensor))
  {
	     stop=j-1;
		 break;
		 }
		 else if((""+s.charAt(j)).equals(delimEnd))
		 {
		   stop=j;
		 break;
		 }

    }
	 int marker=k+2+(""+next).length();
    String data="";
    if(marker<stop){
      data=s.substring(marker,stop);
                    }
 else
     return null;

              String[] temp = data.split(","); //Placing Data in Array--HARD CODE based on ValuesOne, etc....
        int spot=SensorTypeOne*ValuesOne+SensorTypeTwo*ValuesTwo+(next-1-SensorTypeOne-SensorTypeTwo)*ValuesThree;
        int leng=temp.length;
        if (leng>ValuesThree){
            leng=ValuesThree;
        }
       for (int h=0;h<leng;h++)
        {
              int checker=1; //check if each sensor return value is valid
  for(int jk=0;jk<temp[h].length();jk++)
  {
     int asciival=(int)temp[h].charAt(jk);
    if((asciival<=47)||(asciival>=58)){
        checker=0;
    }
  }
  if (temp[h].equals(""))
  {
      checker=0;
  }
      
  //System.out.println(temp[h])
  if(checker==1){
       Double d=Double.parseDouble(temp[h])/100;
        value[spot+h+1]=""+d;}
  else if (checker==0){
      
     value[spot+h+1]="#N/A";
  }
  
        }

//2438, 18B20, 2406 ('0k' or 'nok')
//7,1,5
        //18B20--19 and temp (e.g 2400)
                }
    }
        }
}

     if(s.contains(delimTime))
     {
     int over=0;
     int pos=s.indexOf(delimTime)+4;
     for(int h=pos;h<s.length();h++){

         if((""+s.charAt(h)).equals(delimEnd))
                 over=h;
         }

     if(pos<over)
     {
         Double d=Double.parseDouble(s.substring(pos,over));
         value[0]=""+d;
         }
     }


	  for(int k=0;k<value.length;k++)
	 {
			if(value[k]==null||value[k].equals(""))
					value[k]=nothing;
	}
    //  return returnArrayRow("");
System.out.println("try");
       return value;
        }

    catch(Exception e){
e.printStackTrace();
   System.out.println("catch");
   return returnArrayRow("");
    }
    }

//str[0]="1,";
//str[1]="2,";
//str[2]="3,";
//for(int k=0;k<len;k++)
//index[k] =s.indexOf(str[k]);
//
//for(int k=0;k<len;k++)
//{
//if(index[k]<0)
//value[k]="#N/A";
//else
//{
//int num=numMore(s,index[k],c);
//
//value[k]=s.substring(index[k]+str[k].length(),num );
//}
//}
//for(int k=0;k<value.length;k++)
//{
//if( value[k].length()<1)
//{
//value[k]="#N/A";
//}
//}



public static int lenStr(String s, String delimSensor,int SensorTypeOne,int ValuesOne,int SensorTypeTwo,int ValuesTwo)
{
    
int count=0;
for(int k=0;k<s.length();k++)
{
if((""+s.charAt(k)).equals(delimSensor))
{
int next=Integer.parseInt(""+s.charAt(k+1));
if(next<=SensorTypeOne)
    count+=ValuesOne;
else
    count+=ValuesTwo;
}
    }
//count+=1;

return count;
}

public static int indexFrom(String s, String chars)//returns the next index of a character from the chars string
    {
    for(int i=0;i<s.length();i++)
        if(chars.indexOf(s.charAt(i))>=0)
            return i;
    return -1;


    }


public static int lenStrOld(String s, char c)
{
int count=0;
for(int k=0;k<s.length();k++)
{
if((""+s.charAt(k)).equals(""+c))
count+=1;
}

return count;
}


public static int numMore(String s,int k, char c)
{
int len=s.length();
int pos=0;
while((!(""+s.charAt(pos)).equals(""+c))||pos<=k)
{
pos+=1;
if (pos==len)
    return 0;
   }
return pos;

    }

public static String[][] combine(String [][] init, String[]s)
{
int rows=init.length;
int cols=init[0].length;
String [][] newArray= new String[rows+1][cols];
if(!(cols==(s.length)))
{
//System.out.println("Error in combine method: unequal array lengths");
return init;
}
else
{

for(int r=0; r<rows+1;r++)
{
for(int c=0; c<cols;c++)
{
if(r==rows)
newArray[r][c]=s[c];
else
newArray[r][c]=init[r][c];
}
}
}
return newArray;
}

public static double convertTemp(int [] array)
    {

return 0;

}
public static double convertVolt(int [] array)
    {

return 0;

}
public static double convertCurrent(int [] array)
    {

return 0;

}

public static String stringToHex(String arg) //This method DOES work
{
    return String.format("%x",new BigInteger(arg.getBytes()));
}

public static int hexToInt(String s){ //This method DOES work
int i=Integer.valueOf(s,16).intValue();
return i;

}

public static String [] splitString (String s)
{
return null;


}

public static void doIt(String s ,javax.swing.JTextArea jTextArea2/*, String [][] oldArray*/)
{
    //NOTE: Append to Existing array output commented by //@

//s="";
//s="18,#1,110,120,130,#2,210,220,230,#3,310,320,330,#4,410,420,430,#5,510,520,530,#6,610,620,630,#7,710,720,730,#8,810,820,830,#9,910,920,930,#10,1010,1020,1030,#11,1110,1120,1130,#12,1210,1220,1230;11,4500;";
String [][]oldArray={{"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"},{"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"},{"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"}};
String [] output=returnArrayRow(s); //the meat
//System.out.print(output.length);
//@@ String []output2=new String[output.length];
//@@for (int i=0;i<output.length;i++)
//@@ {
//@@   output2[i]=output[i];
//@@   if(output[i].equals("#N/A")){
//@@   output2[i]="NA";
//@@}
//@@}

//@ System.out.print("RECEIVED STRING:");
//@ for(int k=0;k<output.length;k++)
//@  System.out.print(output[k]+" ");



System.out.println("");
//@ System.out.println("BEGIN APPEND TO OLD ARRAY (THIS IS A TEST--PROBABLY N/A TO THIS PROJECT)");
//for(int r=0;r<oldArray.length;r++)
//for(int c=0;c<oldArray[0].length;c++)
//System.out.print(oldArray[r][c]);

//* System.out.print(output.length+" "+oldArray[0].length);

String [][] newArray=combine(oldArray,output);
//* System.out.println("");

//* for(int r=0;r<newArray.length;r++)
//* for(int c=0;c<newArray[0].length;c++)
//* System.out.print(newArray[r][c]);

//* System.out.println();

String space=", ";
int tab=10;
int arrayRow=newArray.length;
int arrayCol=newArray[0].length;
for(int r=0;r<arrayRow;r++){
for(int c=0;c<arrayCol;c++)
{
int numSpaces=tab-newArray[r][c].length();
String buffer="";
for(int i=0;i<numSpaces;i++)
{
buffer+=" ";
}
//@ System.out.print(newArray[r][c]+space+buffer);
}
//@ System.out.println();
}
//@ System.out.println("END APPEND TO OLD ARRAY (THIS IS A TEST--PROBABLY N/A TO THIS PROJECT)");

String buffer2="";
String outputString="";
for(int i=0;i<output.length;i++)//Output to Text File Formatting
{
int numSpaces=tab-output[i].length();
for(int j=0;j<numSpaces;j++)
{
buffer2+=" ";
}
outputString+=output[i]+space+buffer2;
buffer2="";
}

//@@String buffer3="";
//@@String outputString2="";
//@@for(int i=0;i<output2.length;i++)//Output to GUI Formatting
//@@{
//@@int numSpaces=tab-output2[i].length();
//@@for(int j=0;j<numSpaces;j++)
//@@{
//@@buffer3+=" ";
//@@}
//@@outputString2+=output2[i]+space+buffer3;
//@@buffer3="";
//@@}

System.out.println("OUTPUT TO GUI:"+outputString);
System.out.println("Warning: Interpretation. May be incorrect.");
jTextArea2.append(outputString+"\n");
 try{
 BufferedWriter out=new BufferedWriter (new FileWriter("output.txt",true));
 out.newLine();
 out.write(outputString);
 out.close();
 }
 catch(IOException e){
 e.printStackTrace();
}
// try{
// PrintStream ps = new PrintStream("output.txt");
// ps.println(outputString);
// ps.close();
// }
// catch (Exception e){}
//

}


public static void main(String[] args)//just for test cases
{

Object [] array= {0,3,null,6,3,null,0,9,null,null,10,null};
Object [] cleanedArray=cleanArrayMethod(array);

for(int i=0; i<array.length;i++) //Original
{
System.out.print(array[i]+" ");
}

//Portion 1---Clearing Array of Nulls (completely separate from Portion 2)

System.out.println();

for(int i=0; i<cleanedArray.length;i++) //Final
{
System.out.print(cleanedArray[i]+" ");
}
String [][]oldArray={{"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"},{"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"},{"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"}};

//Portion 2---Outputting/Updating Storage Array

System.out.println();
System.out.println("Part 2");
String s="18,#1,110,120,130,#2,210,220,230,#3,310,320,330,#4,410,420,430,#5,510,520,530,#6,610,620,630,#7,710,720,730,#8,810,820,830,#9,910,920,930,#10,1010,1020,1030,#11,1110,1120,1130,#12,1210,1220,1230;#13,1310,1320,1330,11,4500;";

//doIt(s,null/*,oldArray*/);
}
}
